package com.quectel.cms.controller.partyeducationexam;


import cn.hutool.core.lang.func.LambdaUtil;
import cn.hutool.core.util.EscapeUtil;
import cn.hutool.http.HtmlUtil;
import com.google.common.base.Preconditions;
import com.quectel.business.common.util.sys.SysCodeUtils;
import com.quectel.cms.controller.BaseController;
import com.quectel.cms.util.SessionHolder;
import com.quectel.cms.vo.partyexam.EntrantExamAnswer;
import com.quectel.constant.core.partyeducationexam.PartyEducationExamConstants;
import com.quectel.constant.core.partyeducationexam.PartyEducationExamPersonConstants;
import com.quectel.constant.core.partyeducationexam.PartyEducationQuestionConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.util.aspect.log.annotation.Log;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.constant.sys.SysCodeKeyConstants;
import com.quectel.core.module.partyeducationexam.dto.*;
import com.quectel.core.module.partyeducationexam.dto.ref.ExamRefPersonDto;
import com.quectel.core.module.partyeducationexam.dto.ref.QuestionRefExamDto;
import com.quectel.core.module.partyeducationexam.service.*;
import com.quectel.util.common.*;
import com.quectel.util.kit.PageFetchKit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * @author ryan
 * @email ryan.ran@quectel.com
 * @date 2023/06/16 15:12
 */
@RestController
@RequestMapping("partyEducationQuestion")
@Tag(name = "PartyEducationQuestionController", description = "题目相关api")
public class PartyEducationQuestionController extends BaseController {

    @DubboReference
    private PartyEducationQuestionService partyEducationQuestionService;
    @DubboReference
    private PartyEducationQuestionBankService partyEducationQuestionBankService;
    @DubboReference
    private PartyEducationTrainingExamPersonService partyEducationTrainingExamPersonService;
    @DubboReference
    private PartyEducationTrainingExamPersonHistoryService partyEducationTrainingExamPersonHistoryService;
    @DubboReference
    private PartyEducationTrainingExamQuestionService examQuestionService;
    @DubboReference
    private PartyEducationTrainingExamService partyEducationTrainingExamService;

    @PostMapping("importExcel")
    @RequirePermissions(values = "partyEducationQuestion:import")
    @Operation(summary = "导入题目数据")
    @Log(express = "导入题目数据")
    public Response<Object> importExcel(
            MultipartHttpServletRequest request
    ) {
        Map<String, Object> requestMap = Request.parseParams(request);
        Long bankId = Long.parseLong(requestMap.get(LambdaUtil.getFieldName(PartyEducationQuestionDto::getBankId)) + "");
        PartyEducationQuestionBankDto bankDto = partyEducationQuestionBankService.selectCacheById(bankId);
        if (Objects.isNull(bankDto)) {
            return Response.error("题库信息获取失败");
        }

        int total = 0;
        int sc = 0;

        List<PartyEducationQuestionDto> list = new ArrayList<>();

        Iterator<String> fileNames = request.getFileNames();
        while (fileNames.hasNext()) {
            String next = fileNames.next();
            List<MultipartFile> files = request.getFiles(next);
            for (MultipartFile file : files) {
                try {
                    //校验楼栋数据合法性
                    List<Map<Integer, String>> analysis = POIUtils.analysis(file.getInputStream(), 1, 0);
                    total += analysis.size();

                    for (Map<Integer, String> map : analysis) {
                        try {
                            Byte questionType = SysCodeUtils.easyToGetValue(SysCodeKeyConstants.EXAM_QUESTION_TYPE, map.get(1), Byte.class);
                            if (Objects.isNull(questionType)) {
                                continue;
                            }

                            PartyEducationQuestionDto partyEducationQuestionDto = new PartyEducationQuestionDto();
                            fillSaveCommonData(partyEducationQuestionDto);
                            partyEducationQuestionDto.setTenantId(bankDto.getTenantId());
                            partyEducationQuestionDto.setPartyId(bankDto.getPartyId());
                            partyEducationQuestionDto.setBankId(bankDto.getId());
                            partyEducationQuestionDto.setName(map.get(0));
                            if (PartyEducationQuestionConstants.QuestionTypeEnum.RADIO.getType().equals(questionType)
                                    || PartyEducationQuestionConstants.QuestionTypeEnum.MULTIPLE_CHOICE.getType().equals(questionType)) {
                                List<ExamQuestionOptionCls> ops = new ArrayList<>();
                                for (int i = 3; i <= 8; i++) {
                                    String o = map.get(i);
                                    if (StringUtils.isBlank(o)) {
                                        continue;
                                    }
                                    ExamQuestionOptionCls examQuestionOptionCls = new ExamQuestionOptionCls();
                                    examQuestionOptionCls.setKey(Character.toString((char) (62 + i)));
                                    examQuestionOptionCls.setContent(o);
                                    examQuestionOptionCls.setPicUrl("");
                                    ops.add(examQuestionOptionCls);
                                }
                                partyEducationQuestionDto.setQuestionOption(JacksonUtils.toJsonString(ops));
                            }
                            partyEducationQuestionDto.setType(questionType);
                            partyEducationQuestionDto.setTopic(map.get(2));
                            if (PartyEducationQuestionConstants.QuestionTypeEnum.DETERMINE.getType().equals(questionType)) {
                                if (SystemConstants.RIGHT_CHINESE.equals(map.get(9))) {
                                    partyEducationQuestionDto.setAnswer(JacksonUtils.toJsonString(Collections.singletonList(SystemConstants.ENABLE.toString())));
                                } else if (SystemConstants.WRONG_CHINESE.equals(map.get(9))) {
                                    partyEducationQuestionDto.setAnswer(JacksonUtils.toJsonString(Collections.singletonList(SystemConstants.DISABLE.toString())));
                                } else {
                                    // 判断题，未知答案
                                    continue;
                                }
                            } else {
                                partyEducationQuestionDto.setAnswer(JacksonUtils.toJsonString(map.get(9).split(",")));
                            }
                            partyEducationQuestionDto.setAnalysis(map.get(10));
                            if (PartyEducationQuestionConstants.QuestionTypeEnum.FILL_BLANKS.getType().equals(questionType)) {
                                if (StringUtils.isNotBlank(map.get(11))) {
                                    partyEducationQuestionDto.setInOrder(PartyEducationQuestionConstants.NeedOrderEnum.getStatusByDesc(map.get(11)));
                                } else {
                                    partyEducationQuestionDto.setInOrder(PartyEducationQuestionConstants.NeedOrderEnum.NEED.getOrderStatus());
                                }
                            }
                            if (PartyEducationQuestionConstants.QuestionTypeEnum.SHORT_ANSWER.getType().equals(questionType)) {
                                if (StringUtils.isNotBlank(map.get(12))) {
                                    partyEducationQuestionDto.setKeywords(JacksonUtils.toJsonString(map.get(12).split(",")));
                                }
                            }
                            partyEducationQuestionDto.setClassify(bankDto.getClassify());
                            partyEducationQuestionDto.setStatus(SysCodeUtils.easyToGetValue(SysCodeKeyConstants.COMMON_STATUS, map.get(13), Byte.class));
                            list.add(partyEducationQuestionDto);
                            sc++;
                        } catch (Exception e) {
                            logger.error("题目导入异常", e);
                        }
                    }
                } catch (Exception e) {
                    logger.error("解析题目数据出错");
                }
            }
        }

        partyEducationQuestionService.saveBatch(list);
        return Response.error(String.format("导入完成 总数%d 成功数%d", total, sc));
    }

    /**
     * 导出题目列表
     */
    @GetMapping("exportExcel")
    @RequirePermissions(values = "partyEducationQuestion:export")
    @Operation(summary = "导出题目列表")
    public void exportExcel(@RequestParam Map<String, Object> params,
                                    HttpServletResponse resp) {
        super.fillAuthorityData(params);
        List<PartyEducationQuestionDto> partyEducationQuestionDtos = PageFetchKit.fetchAll(req -> partyEducationQuestionService.queryList(req), params, 500);
        String[] headers = new String[]{"所属党组织", "题库名称", "题目名称", "题目分类", "题目类型", "问题", "选项A","选项B","选项C","选项D","选项E","选项F", "答案", "解析", "是否有序", "答案关键字", "状态", "创建时间"};
        List<String[]> dataList = new ArrayList<>();
        for (PartyEducationQuestionDto data : partyEducationQuestionDtos) {
            String[] item = new String[headers.length];
            item[0] = Objects.nonNull(data.getParty()) ? data.getParty().getName() : "";
            item[1] = Objects.nonNull(data.getQuestionBank()) ? data.getQuestionBank().getName() : "";
            item[2] = data.getName();
            item[3] = SysCodeUtils.easyToGetName(SysCodeKeyConstants.EXAM_QUESTION_BANK_CLASSIFY, Objects.nonNull(data.getQuestionBank()) ? data.getQuestionBank().getClassify() + "" : "");
            item[4] = SysCodeUtils.easyToGetName(SysCodeKeyConstants.EXAM_QUESTION_TYPE, data.getType() + "");
            item[5] = Objects.nonNull(data.getTopic()) ? HtmlUtil.cleanHtmlTag(EscapeUtil.unescapeHtml4(data.getTopic())) : "";
            if (PartyEducationQuestionConstants.QuestionTypeEnum.RADIO.getType().equals(data.getType())
                    || PartyEducationQuestionConstants.QuestionTypeEnum.MULTIPLE_CHOICE.getType().equals(data.getType())) {
                // 单选题和多选题的六个选项
                List<ExamQuestionOptionCls> list = Objects.nonNull(data.getQuestionOption()) ? JacksonUtils.parseArray(data.getQuestionOption(), ExamQuestionOptionCls.class) : Lists.newArrayList();
                list.sort(Comparator.comparing(ExamQuestionOptionCls::getKey));
                for (int i = 1; i <= 6; i++) {
                    String o = "";
                    if (i <= list.size()) {
                        ExamQuestionOptionCls oc = list.get(i - 1);
                        if (oc != null) {
                            if (StringUtils.isNotBlank(oc.getContent())) {
                                o += oc.getContent();
                            }
                            if (StringUtils.isNotBlank(oc.getPicUrl())) {
                                if (StringUtils.isNotBlank(o)) {
                                    o += ",";
                                }
                                o += oc.getPicUrl();
                            }
                        }
                    }
                    item[5 + i] = o;
                }
            }
            item[12] = StringUtils.isNotBlank(data.getAnswer()) ? String.join(",", JacksonUtils.parseArray(data.getAnswer(), String.class)) : "";
            item[13] = Objects.nonNull(data.getAnalysis()) ? HtmlUtil.cleanHtmlTag(EscapeUtil.unescapeHtml4(data.getAnalysis())) : "";
            item[14] = Objects.nonNull(PartyEducationQuestionConstants.NeedOrderEnum.getDescByStatus(data.getInOrder())) ? PartyEducationQuestionConstants.NeedOrderEnum.getDescByStatus(data.getInOrder()) : "";
            item[15] = StringUtils.isNotBlank(data.getKeywords()) ? String.join(",", JacksonUtils.parseArray(data.getKeywords(), String.class)) : "";
            item[16] = SysCodeUtils.easyToGetName(SysCodeKeyConstants.COMMON_STATUS, data.getStatus() + "");
            item[17] = Objects.nonNull(data.getCreateTime()) ? DateUtils.format(data.getCreateTime(), DateUtils.FormatType.COMMON) : "";
            dataList.add(item);
        }
        POIUtils.exportExcel(resp, "题目列表情况", headers, dataList, "题目列表情况");
    }

    @PostMapping("grade")
    @Operation(summary = "阅卷")
    public Response<Object> grade(@RequestBody @Validated EntrantExamAnswer examAnswer){
        PartyEducationTrainingExamPersonHistoryDto partyEducationTrainingExamPersonHistoryDto = partyEducationTrainingExamPersonHistoryService.selectById(examAnswer.getId());
        if (Objects.isNull(partyEducationTrainingExamPersonHistoryDto)) {
            return Response.error("考试记录不存在");
        }
        if (!PartyEducationExamPersonConstants.PassStatusEnum.UNRECORDED.getStatus().equals(partyEducationTrainingExamPersonHistoryDto.getPassStatus())) {
            return Response.error("试卷已审阅过，无需重复审阅");
        }
        List<QuestionRefExamDto> questions = examAnswer.getQuestions();
        // 只有简答题需要阅卷
        List<QuestionRefExamDto> shortAnswerList = questions.stream()
                .filter(o -> PartyEducationQuestionConstants.QuestionTypeEnum.SHORT_ANSWER.getType().equals(o.getType()))
                .collect(Collectors.toList());

        PartyEducationTrainingExamPersonHistoryDto updateObj = new PartyEducationTrainingExamPersonHistoryDto();
        updateObj.setId(partyEducationTrainingExamPersonHistoryDto.getId());

        List<EntrantAnswerCls> originAnswerList = JacksonUtils.parseArray(partyEducationTrainingExamPersonHistoryDto.getAnswer(), EntrantAnswerCls.class);
        Map<Long, EntrantAnswerCls> answerMap = new HashMap<>();
        for (EntrantAnswerCls entrantAnswerCls : originAnswerList) {
            answerMap.put(entrantAnswerCls.getQuestionId(), entrantAnswerCls);
        }

        BigDecimal score = partyEducationTrainingExamPersonHistoryDto.getScore();
        if (CollectionUtils.isNotEmpty(shortAnswerList)) {
            for (QuestionRefExamDto questionRefExamDto : shortAnswerList) {
                if (questionRefExamDto.getEntrantScore() != null
                        && questionRefExamDto.getEntrantScore().compareTo(BigDecimal.ZERO) > 0
                        && questionRefExamDto.getEntrantScore().compareTo(questionRefExamDto.getScore()) <= 0) {
                    score = score.add(questionRefExamDto.getEntrantScore());
                }

                EntrantAnswerCls entrantAnswerCls = answerMap.get(questionRefExamDto.getId());
                if (entrantAnswerCls != null) {
                    entrantAnswerCls.setGrade(PartyEducationQuestionConstants.GradeEnum.WRONG.getStatus());
                    entrantAnswerCls.setScore(questionRefExamDto.getEntrantScore());
                    if (entrantAnswerCls.getScore() != null && entrantAnswerCls.getScore().compareTo(questionRefExamDto.getScore()) >= 0) {
                        entrantAnswerCls.setGrade(PartyEducationQuestionConstants.GradeEnum.RIGHT.getStatus());
                    }
                    answerMap.put(entrantAnswerCls.getQuestionId(), entrantAnswerCls);
                }
            }
            updateObj.setScore(score);
        }

        PartyEducationTrainingExamDto partyEducationTrainingExamDto = partyEducationTrainingExamService.selectCacheById(partyEducationTrainingExamPersonHistoryDto.getExamId());
        Preconditions.checkNotNull(partyEducationTrainingExamDto);

        updateObj.setPassStatus(score.compareTo(new BigDecimal(partyEducationTrainingExamDto.getPassScore() + "")) >= 0
                ? PartyEducationExamPersonConstants.PassStatusEnum.PASSED.getStatus()
                : PartyEducationExamPersonConstants.PassStatusEnum.FAIL.getStatus());

        updateObj.setAnswer(JacksonUtils.toJsonString(answerMap.values()));
        updateObj.setGradeTime(new Date());
        updateObj.setGradePersonName(Objects.requireNonNull(SessionHolder.getSysUser()).getUsername());
        partyEducationTrainingExamPersonHistoryService.updateById(updateObj);

        PartyEducationTrainingExamPersonDto examPersonDto = partyEducationTrainingExamPersonService.selectOneByLastExam(partyEducationTrainingExamPersonHistoryDto.getId());
        if (Objects.nonNull(examPersonDto)) {
            PartyEducationTrainingExamPersonDto updateRefObj = new PartyEducationTrainingExamPersonDto();
            updateRefObj.setId(examPersonDto.getId());
            if (!PartyEducationExamPersonConstants.PassStatusEnum.PASSED.getStatus().equals(examPersonDto.getPassStatus())) {
                updateRefObj.setPassStatus(updateObj.getPassStatus());
            }
            updateRefObj.setLastScore(score);
            if (examPersonDto.getMaxScore() == null || score.compareTo(examPersonDto.getMaxScore()) > 0) {
                updateRefObj.setMaxScore(score);
            }
            partyEducationTrainingExamPersonService.updateById(updateRefObj);
        }
        return Response.ok();
    }

    @GetMapping("answerList")
    @Operation(summary = "考生答题列表")
    public Response<EntrantExamAnswer> wrongList(@Parameter(description = "考生 id") @RequestParam Long personId,
                                                 @Parameter(description = "考试 id") @RequestParam Long examId) {
        Map<String, Object> params = new HashMap<>();
        params.put(LambdaUtil.getFieldName(PartyEducationTrainingExamPersonDto::getPartyPersonId), personId);
        params.put(LambdaUtil.getFieldName(PartyEducationTrainingExamDto::getStatus), PartyEducationExamConstants.StatusEnum.PUBLISHED.getStatus());
        params.put(LambdaUtil.getFieldName(PartyEducationTrainingExamPersonDto::getExamId), examId);
        List<ExamRefPersonDto> examRefPersonDtos = partyEducationTrainingExamPersonService.queryExamList(params);
        if (CollectionUtils.isEmpty(examRefPersonDtos)) {
            return Response.error("考试不存在");
        }
        ExamRefPersonDto examPersonDto = examRefPersonDtos.get(0);
        if (PartyEducationExamPersonConstants.TakeStatusEnum.UNEXAMINED.getStatus().equals(examPersonDto.getTakeStatus())) {
            return Response.error("考生还未完成考试");
        }
        PartyEducationTrainingExamPersonHistoryDto partyEducationTrainingExamPersonHistoryDto = partyEducationTrainingExamPersonHistoryService.selectCacheById(examPersonDto.getLastExamId());

        List<EntrantAnswerCls> entrantAnswerList = JacksonUtils.parseArray(partyEducationTrainingExamPersonHistoryDto.getAnswer(), EntrantAnswerCls.class);
        Map<Long, EntrantAnswerCls> map = new HashMap<>();
        for (EntrantAnswerCls entrantAnswerCls : entrantAnswerList) {
            map.put(entrantAnswerCls.getQuestionId(), entrantAnswerCls);
        }

        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put(LambdaUtil.getFieldName(PartyEducationTrainingExamQuestionDto::getExamId), partyEducationTrainingExamPersonHistoryDto.getExamId());
        List<QuestionRefExamDto> questionRefExamDtos = examQuestionService.queryQuestionList(queryParams);
        List<QuestionRefExamDto> respQuestions = questionRefExamDtos.parallelStream()
                .filter(o -> map.containsKey(o.getId()))
                .sorted(Comparator.comparing(PartyEducationQuestionDto::getType))
                .collect(Collectors.toList());
        for (QuestionRefExamDto questionRefExamDto : respQuestions) {
            EntrantAnswerCls entrantAnswerCls = map.get(questionRefExamDto.getId());
            if (Objects.isNull(entrantAnswerCls)) {
                continue;
            }
            questionRefExamDto.setEntrantScore(entrantAnswerCls.getScore());
            questionRefExamDto.setEntrantAnswer(JacksonUtils.toJsonString(entrantAnswerCls.getEntrantAnswer()));
            questionRefExamDto.setGrade(entrantAnswerCls.getGrade());
        }

        EntrantExamAnswer resp = new EntrantExamAnswer();
        resp.setId(partyEducationTrainingExamPersonHistoryDto.getId());
        resp.setQuestions(respQuestions);
        resp.setPersonName(examPersonDto.getPersonName());
        resp.setPartyName(Objects.nonNull(examPersonDto.getParty()) ? examPersonDto.getParty().getName() : "");
        resp.setGradePersonName(partyEducationTrainingExamPersonHistoryDto.getGradePersonName());
        resp.setGradeTime(partyEducationTrainingExamPersonHistoryDto.getGradeTime());
        resp.setScore(partyEducationTrainingExamPersonHistoryDto.getScore());
        return Response.<EntrantExamAnswer>ok().wrap(resp);
    }

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    @RequirePermissions(values = "partyEducationQuestion:list")
    @Operation(summary = "分页获取题目列表")
    public Response<Page<PartyEducationQuestionDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = partyEducationQuestionService.queryTotal(request.getParams());

        List<PartyEducationQuestionDto> list = null;
        if (total > 0) {
            list = partyEducationQuestionService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<PartyEducationQuestionDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<PartyEducationQuestionDto>>ok().wrap(page);
    }

    /**
     * 编辑查询反显
     *
     * @param id
     * @return
     */
    @GetMapping("infoForEdit/{id}")
    @RequirePermissions(values = "partyEducationQuestion:edit")
    @Operation(summary = "在编辑时根据id查询题目详情")
    public Response<PartyEducationQuestionDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (partyEducationQuestionService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        PartyEducationQuestionDto partyEducationQuestionDto = partyEducationQuestionService.selectById(id);

        return Response.<PartyEducationQuestionDto>ok().wrap(partyEducationQuestionDto);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "partyEducationQuestion:info")
    @Operation(summary = "根据id查询题目的详情")
    public Response<PartyEducationQuestionDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (partyEducationQuestionService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        PartyEducationQuestionDto partyEducationQuestionDto = partyEducationQuestionService.selectById(id);

        return Response.<PartyEducationQuestionDto>ok().wrap(partyEducationQuestionDto);
    }

    /**
     * 保存
     *
     * @param partyEducationQuestionDto
     * @return
     */
    @PostMapping("save")
    @RequirePermissions(values = "partyEducationQuestion:edit")
    @Operation(summary = "新增题目")
    public Response<Object> save(@RequestBody @Validated PartyEducationQuestionDto partyEducationQuestionDto) {
        PartyEducationQuestionBankDto bankDto = partyEducationQuestionBankService.selectCacheById(partyEducationQuestionDto.getBankId());
        if (Objects.isNull(bankDto)) {
            return Response.error("题库不存在");
        }
        partyEducationQuestionDto.setClassify(bankDto.getClassify());
        //填充基础数据
        fillSaveCommonData(partyEducationQuestionDto);

        partyEducationQuestionService.save(partyEducationQuestionDto);

        return Response.ok();
    }


    /**
     * 修改
     *
     * @param partyEducationQuestionDto
     * @return
     */
    @PostMapping("update")
    @RequirePermissions(values = "partyEducationQuestion:edit")
    @Operation(summary = "根据id修改题目")
    public Response<Object> update(@RequestBody  @Validated PartyEducationQuestionDto partyEducationQuestionDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(partyEducationQuestionDto.getId()));
        if (partyEducationQuestionService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(partyEducationQuestionDto);

        partyEducationQuestionService.updateById(partyEducationQuestionDto);

        return Response.ok();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @RequirePermissions(values = "partyEducationQuestion:delete")
    @Operation(summary = "根据多条记录id批量删除题目")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (partyEducationQuestionService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        partyEducationQuestionService.deleteBatch(ids);

        return Response.ok();
    }
}
